package com.exercises;


import java.util.*;

class Exercise46 {
    public static void main(String[] args) {
        System.out.println(new Exercise46().permute(new int[]{2, 2, 1, 1}));

    }


    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        if (nums.length == 1) {
            list.add(Arrays.asList(nums[0]));
        } else {
            List<List<Integer>> chlid = permute(Arrays.copyOf(nums, nums.length - 1));
            chlid.forEach(l -> {
                int size = l.size();

                for (int i = 0; i <= size; i++) {
                    List<Integer> a = new ArrayList<>(l);
                    a.add(i, nums[nums.length - 1]);
                    list.add(a);

                }
            });
        }


        return list;
    }

}

